<?php

App::uses('AppModel', 'Model');
App::uses('CompositeKeyModel', 'Model');

/**
 * Member Model
 *
 * @property Organisation $Organisation
 * @property MemberBiodata $MemberBiodata
 * @property MemberChild $MemberChild
 * @property OrganisationMemberContribution $OrganisationMemberContribution
 * @property OrganisationMemberAnniversary $OrganisationMemberAnniversary
 * @property OrganisationMemberGroup $OrganisationMemberGroup
 * @property MemberParent $MemberParent
 * @property MemberSpouse $MemberSpouse
 */
class Member extends CompositeKeyModel {
    
    public $virtualFields = array(
        'full_name' => 'CONCAT(Member.last_name, " ", Member.first_name)',
        'age' => 'FLOOR(DATEDIFF(NOW(), dob) / 365.25)'
    );
    
    public $useDbConfig = "organisations";
    
    //The Associations below have been created with all possible keys, those that are not needed can be removed
    
    /**
     * belongsTo associations
     *
     * @var array
     */
    public $hasOne = array(
        'MemberImage' => array(
            'className' => 'MemberImage',
            'foreignKey' => 'member_id',
            'conditions' => '',
            'fields' => '',
            'order' => 'MemberImage.id desc'
        ),
        'MemberAccount' => array(
            'className' => 'MemberAccount',
            'foreignKey' => 'member_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    /**
     * hasMany associations
     *
     * @var array
     */
    public $hasMany = array(
        'MemberBiodata' => array(
            'className' => 'MemberBiodata',
            'foreignKey' => 'member_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'MemberChild' => array(
            'className' => 'MemberChild',
            'foreignKey' => 'member_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'MemberParent' => array(
            'className' => 'MemberParent',
            'foreignKey' => 'member_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'MemberSpouse' => array(
            'className' => 'MemberSpouse',
            'foreignKey' => 'member_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'OrganisationMember' => array(
            'className' => 'OrganisationMember',
            'foreignKey' => 'member_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    public function getTotalMembers($organisation_id) {
        return $this->find('count', array(
            'conditions' => array(
                'Member.organisation_id' => $organisation_id,
                'Member.active' => 1
            )
        ));
    }
    
    public function getTotalMembersByGender($organisation_id, $gender) {
        return $this->find('count', array(
            'conditions' => array(
                'Member.organisation_id' => $organisation_id,
                'Member.gender' => $gender,
                'Member.active' => 1
            )
        ));
    }
    
    public function findMembersBySearchTerm($organisation_id, $term) {
        
        $conditions = array(
            'or' => array(
                'Member.first_name LIKE' => "%$term%",
                'Member.last_name LIKE' => "%$term%",
                'Member.email LIKE' => "%$term%",
                "REPLACE(REPLACE(Member.mobile_number, ' ', ''), '-','') LIKE" => "%" . str_replace("-", '', str_replace(" ", '', $term)) . "%"
            ),
            'active' => 1,
            'organisation_id' => $organisation_id
        );
        
        $order = array('Member.last_name' => 'asc');

        return $this->find('all', array(
            'fields' => array('id', 'first_name', 'last_name', 'gender', 'email', 'dob', 'organisation_id', 'organisation_no', 'mobile_number'),
            'conditions' => $conditions,
            'order' => $order,
            'limit' => 100,
            'recursive' => -1
        ));
    }
    
    /**
     * Flags the specified member as deleted in the organisation
     * 
     * @param type $organisation_id
     * @param type $member_id
     * @return type
     */
    public function deleteMember($organisation_id, $member_id) {
        return $this->save(array(
            'id' => $member_id,
            'organisation_id' => $organisation_id,
            'active' => -1
        ));
    }
}
